<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-left">
        <el-input
          size="mini"
          v-model="searchKey"
          placeholder="请输入Json名称查询"
          style="width: 250px"
          class="filter-item"
          @keyup.enter.native="handleFilter"
          @input="handleFilter"
        />
        <!-- <el-button
          size="mini"
          v-waves
          class="filter-item"
          style="margin-left: 10px"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >
          查询
        </el-button> -->
        <el-button
          size="mini"
          v-waves
          class="filter-item"
          style="margin-left: 10px"
          type="success"
          :loading="userLoading"
          @click="updateOnJobUserLists"
        >
          更新在职员工列表
        </el-button>
        <el-button
          size="mini"
          v-waves
          class="filter-item"
          style="margin-left: 10px"
          type="success"
          :loading="deptTreeLoading"
          @click="updateGetDeptTree"
        >
          更新部门列表树
        </el-button>
      </div>
      <div class="filter-right">
        <el-button icon="el-icon-plus" type="primary" size="mini" @click="handleAdd">
          新增配置
        </el-button>
      </div>
    </div>
    <el-table
      :data="pageList"
      border
      stripe
      fit
      highlight-current-row
      v-loading="listLoading"
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="json_name" label="JSON名称" align="center">
      </el-table-column>
      <el-table-column
        label="JSON详情"
        align="center"

      >
        <template slot-scope="{row}">
          <div class="jsonData">{{ row.json_data }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="json_code" label="JSON编码" align="center">
      </el-table-column>

      <el-table-column label="创建时间" width="200px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.created_at | formate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" width="200px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.updated_at | formate }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            style="color: green"
            @click="handleDetails(scope.row)"
            >查看详情</el-button
          >
          <el-button type="text" size="small" @click="handleModify(scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getPageList"
    />
  </div>
</template>
   
  <script>
import { getJsonConfigList, getOnJobUserLists, getDeptTree } from "@/api/configuration";
import { checkUserToken } from "@/api/user";
import waves from "@/directive/waves"; // waves directive
import { curentTime } from "@/filters/index";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

export default {
  // 注册组件
  components: { Pagination },
  directives: { waves },
  filters: {
    formate(date) {
      return curentTime(date);
    },
  },
  data() {
    return {
      // 品牌数据
      pageList: [],
      // 品牌总数
      total: 0,
      // 加载
      listLoading: true,
      // 查询条件
      listQuery: {
        pageNum: 1,
        pageSize: 10,
      },
      searchKey: "",
      userLoading: false,
      deptTreeLoading: false
    };
  },
  async created() {
    await this.getPageList();
  },
  methods: {
    //  更新部门列表树
    async updateGetDeptTree(){
      if(this.deptTreeLoading) return;
      this.deptTreeLoading = true;
      const res = await getDeptTree();
      this.deptTreeLoading = false;
      if(res && res.success){
        this.$message({
          message: "部门列表树更新成功",
          type: "success",
        });
      }else{
        this.$message({
          message: "部门列表树更新失败",
          type: "fail",
        });
      }
      //  刷新列表
      await this.getPageList();
    },
    // 更新在职员工列表
    async updateOnJobUserLists(){
      if(this.userLoading) return;
      this.userLoading = true;
      const res = await getOnJobUserLists();
      this.userLoading = false;
      if(res && res.success){
        this.$message({
          message: "在职员工列表更新成功",
          type: "success",
        });
      }else{
        this.$message({
          message: "在职员工列表更新失败",
          type: "fail",
        });
      }
      //  刷新列表
      await this.getPageList();
    },
    // 按需查询
    async handleFilter() {
      this.listQuery.searchKey = this.searchKey;
      this.listQuery.pageNum = 1;
      await this.getPageList();
    },

    // 查询列表
    async getPageList() {
      try {
        const res = await getJsonConfigList(this.listQuery);
        this.total = res.data.total;
        this.pageList = res.data.lists;
        this.listLoading = false;
      } catch (error) {
        console.log(error);
      }
    },

    // 新增
    handleAdd() {
      checkUserToken().then((res) => {
        if(res.data) {
          this.$router.push({ path: "/configuration/details" });
        }
      })
    },

    // 修改
    handleModify(row) {
      this.$router.push({
        path: "/configuration/details",
        query: {
          type: "amend",
          id: row.id,
          code: row.json_code,
          name: row.json_name,
        },
      });
    },

    // 查看详情
    handleDetails(row) {
      this.$router.push({
        path: "/configuration/details",
        query: {type:'view', id: row.id, code: row.json_code, name: row.json_name },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
}

.jsonData {
  width: 100%;
  height: 70px;
  overflow: hidden;
}
</style>

